package com.zhixianggou.caibaobox.commons.result;

import lombok.Data;

import java.util.List;

/**
 * @Classname PageDto
 * @Date 2020/5/5 7:32 PM
 * @Created by wxdl
 * @Email xuwenlong@shike8888.com
 * @Description 分页对象
 */

@Data
public class PageBean<T> {

    private Integer totalNum;

    private Integer totalPage;

    private Integer pageSize;

    private Integer pageNum;

    private Integer startNum;

    private List<T> data;

    public PageBean(Integer pageSize, Integer pageNum) {
        this.pageSize = null == pageSize || pageSize <= 0 ? 20 : pageSize;
        this.pageNum = null == pageNum || pageNum <= 0 ? 1 : pageNum;
        this.startNum = (this.pageNum - 1) * this.pageSize;
    }

    public PageBean() {

        this.pageSize = 20;
        this.pageNum = 1;
        this.startNum = 0;
    }

    public void setPageSize(Integer pageSize) {

        this.pageSize = null == pageSize || pageSize <= 0 ? 20 : pageSize;
        this.pageNum = null == pageNum || pageNum <= 0 ? 1 : pageNum;
        this.startNum = (pageNum - 1) * (this.pageSize);
    }

    public void setTotalNum(Integer totalNum) {
        this.totalNum = totalNum;

        if (null != this.totalNum && this.totalNum > 0) {
            this.totalPage = (int) this.totalNum / this.pageSize + 1;
        } else {
            this.totalPage = 0;
        }
    }

    public static PageBean initPage(PageBean page) {

        if (null == page) {
            page = new PageBean();
        }

        page.check();
        return page;
    }


    public void check() {

        if (null == pageNum || 0 == pageNum) {
            pageNum = 1;
        }

        if (null == pageSize || 0 == pageSize) {
            pageSize = 20;
        }

//        this.setStartNum((pageNum-1)*pageSize);
    }
}
